package mest.controller.cxj;


import com.alibaba.fastjson.JSON;
import mest.VO.cxj.OtherIncomeVO;
import mest.entity.DictionariesOrder;
import mest.entity.otherincome;
import mest.service.cxj.DictionariesOrderService;
import mest.service.cxj.OtherIncomeService;
import mest.service.system.DBService;
import mest.util.NumberConst;
import mest.util.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("otherIncome")
@CrossOrigin
public class OtherIncomeController {

    @Autowired
    private OtherIncomeService otherIncomeService;

    @Autowired
    private DictionariesOrderService dictionariesOrderService;

    @Autowired
    private DBService dbService;

    /**
     * 生成其他收入单编码
     * @return
     */
    @GetMapping("getCode")
    private String getCode(){
        String theirDb=dbService.getCompanyDB();
        DictionariesOrder dictionariesOrder = dictionariesOrderService.selectDictionariesOrder(theirDb);
        int otherIncomeNumber = dictionariesOrder.getOtherIncomeNumber();
        return NumberConst.PlanOtherIncome_No + "-" + otherIncomeNumber;
    }

    /**
     * 新增其他收入单据
     * @param oi
     * @return
     */
    @PostMapping("insert")
    public String insert(@RequestBody otherincome oi) {
        ResultObj resultObj = null;
        String theirDb=dbService.getCompanyDB();
        try {
            oi.setEfId(1);//设置默认操作人
            if (otherIncomeService.insertOtherIncome(oi,theirDb) > 0) {
                dictionariesOrderService.updateDictionariesOrderByOtherIncomeNumber(theirDb);
                resultObj = ResultObj.builder().code(0).msg("添加成功").build();
            } else {
                resultObj = ResultObj.builder().code(-1).msg("添加失败").build();
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultObj = ResultObj.builder().code(-2).msg("系统异常,请联系管理员").build();
        }
        return JSON.toJSONString(resultObj);
    }

    /**
     * 修改其他收入单据
     * @param oi
     * @return
     */
    @PostMapping("modify")
    public String modify(@RequestBody otherincome oi) {
        ResultObj resultObj = null;
        String theirDb=dbService.getCompanyDB();
        try {
            if (otherIncomeService.modifyOtherIncome(oi,theirDb) > 0) {
                resultObj = ResultObj.builder().code(0).msg("修改成功").build();
            } else {
                resultObj = ResultObj.builder().code(-1).msg("修改失败").build();
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultObj = ResultObj.builder().code(-2).msg("系统异常,请联系管理员").build();
        }
        return JSON.toJSONString(resultObj);
    }

    /**
     * 查询其他首页
     * @param page
     * @param limit
     * @param content
     * @return
     */
    @GetMapping("selectAll")
    public String selectAll(@RequestParam Integer page, @RequestParam Integer limit,
                            @RequestParam(required = false) String content) {
        ResultObj resultObj = null;
        String theirDb=dbService.getCompanyDB();
        try {
            List<OtherIncomeVO> otherIncomeVOList = otherIncomeService.selectAllOtherIncome(page, limit, content,theirDb);
            Integer count = otherIncomeService.selectCountOtherIncome(content,theirDb);
            resultObj = new ResultObj(0, "success", count, otherIncomeVOList);
        } catch (Exception e) {
            e.printStackTrace();
            resultObj = ResultObj.builder().code(-2).msg("系统异常,请联系管理员").build();
        }
        return JSON.toJSONString(resultObj);
    }

    /**
     * 删除费用单（假删）
     *
     * @param id
     * @return
     */
    @DeleteMapping("delete")
    public String delete(@RequestParam Integer id) {
        ResultObj resultObj = null;
        String theirDb=dbService.getCompanyDB();
        try {
            if (otherIncomeService.deleteOtherIncome(id,theirDb)>0) {
                resultObj = ResultObj.builder().code(0).msg("删除成功").build();
            } else {
                resultObj = ResultObj.builder().code(-1).msg("删除失败").build();
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultObj = ResultObj.builder().code(-2).msg("系统异常,请联系管理员").build();
        }
        return JSON.toJSONString(resultObj);
    }
}
